package com.xxx.video.commen.utils;

import com.google.android.material.appbar.AppBarLayout;

public abstract class AppBarLayoutStateChangeListener
        implements AppBarLayout.OnOffsetChangedListener {

    public  enum State {
        /**
         * open
         */
        EXPANDED,
        /**
         * close
         */
        COLLAPSED,
        INTERMEDIATE
    }

    private State mCurrentState = State.INTERMEDIATE;

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (verticalOffset == 0) {
            if (mCurrentState != State.EXPANDED) {
                onStateChanged(appBarLayout, State.EXPANDED);
            }
            mCurrentState = State.EXPANDED;
        } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
            if (mCurrentState != State.COLLAPSED) {
                onStateChanged(appBarLayout, State.COLLAPSED);
            }
            mCurrentState = State.COLLAPSED;
        } else {
            if (mCurrentState != State.INTERMEDIATE) {
                onStateChanged(appBarLayout, State.INTERMEDIATE);
            }
            mCurrentState = State.INTERMEDIATE;
        }
    }

    /**
     * listener
     *
     * @param appBarLayout appbar layout
     * @param state        state
     */
    public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
}
